Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fix valset pref unbonding bugs #5967

Merged
merged 35 commits into from
Oct 13, 2023
Merged

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Aug 6, 2023

Closes: #6028

This PR is aiming to fix the following two bugs in valset-preference unbonding:

  • Sometimes selecting more to unbond than the user has
  • Correctly unbonding if the user's delegations aren't according to valset

The solution to both is the same: Get the delegator's existing delegation to each val in valset, which does increase gas.

@github-actions github-actions bot added the C:docs Improvements or additions to documentation label Aug 6, 2023
@ValarDragon
Copy link
Member Author

@stackman27 taking over this PR (or should I get this mergeable (just fixing conflicts), and then you implement the remainder in a followup?

@stackman27
Copy link
Contributor

@stackman27 taking over this PR (or should I get this mergeable (just fixing conflicts), and then you implement the remainder in a followup?

its okay i can just work on this PR and have 1 PR that fixes

@stackman27 stackman27 self-assigned this Aug 7, 2023
totalAmountFromWeights = totalAmountFromWeights.Add(val.Weight.Mul(tokenAmt))
}
// Step 7
fmt.Println("Undelegating the remaining amount.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: i'm a little worried about this part of the algorithm. would appreciate a thorough review here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part in specific are you worried about?

targetRatio := sdk.OneDec()
amountRemaining := coin.Amount

fmt.Println("VRATIO PRE: ", valSetRatio)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: these Println are easier to debug/review. Will remove them once this PR is close to merge

@stackman27 stackman27 added V:state/breaking State machine breaking PR A:no-changelog labels Aug 8, 2023
@stackman27 stackman27 marked this pull request as ready for review August 8, 2023 02:53
@stackman27 stackman27 requested a review from mattverse as a code owner August 8, 2023 02:53
@stackman27 stackman27 changed the title WIP: Fix valset pref unbonding bugs Fix: Fix valset pref unbonding bugs Aug 8, 2023
Comment on lines 45 to 46
Algorithm for undelegation;
// unbond as true to valset ratios as possible. Undelegation should be possible.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add my intuition description for the algorithm back!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason all of this is commented?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 143 to 144
// Step 1-2
var valSetRatio []ValRatio
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove the helper I had before? It improves gas costs by removing repeated Get's with GetDelegationPreferences, and makes this main function cleaner?

Copy link
Contributor

@stackman27 stackman27 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did this for couple of reason

  1. the helper func was useful except i wanted current delegations to be represented as valset {valAddr, weight} and the helper func did the opposite. (represented valset as current delegation)
  2. I checked the flow and current func i'm using has less Get calls to staking keeper than the helper function.
(helper = getValset -> if exist -> getDelegation -> else -> GetDelegatorDelegation,

 nonHelper = getValSet -> if exist return -> else -> GetDelegatorDelegations) 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting, didn't notice the delegations call being reused my bad

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, the delegations aren't getting reused rightnow if you don't have a validator set. This is causing more gas usage.

Copy link
Contributor

@stackman27 stackman27 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you dont have a validator set but have existing delegations we create a validator set for you with existing delegations
ref: https://github.com/osmosis-labs/osmosis/pull/5967/files#diff-eca64719739dd13c273495f6d28e8da96083fdf0c629da0eef1a22206839ed24R84

note: we create temporary valset but donot store it in state, to store it in state you just call SetValSetPref. If valSet exist and delegation also exist and you want to store temporary valset in state this implies ValSetRedelegation

@stackman27
Copy link
Contributor

devbot add changelog fix ValSet undelegate API out of sync with existing staking

@stackman27
Copy link
Contributor

stackman27 commented Aug 8, 2023

@ValarDragon i'm a little worried that we might have this issue in redelegation too, since it requires tokens to be unbonded. I haven't dug deep into it but if you think dont think this is the case i can focus on other work

x/valset-pref/README.md Outdated Show resolved Hide resolved
x/valset-pref/validator_set.go Outdated Show resolved Hide resolved
x/valset-pref/validator_set.go Outdated Show resolved Hide resolved
@czarcas7ic czarcas7ic removed their assignment Sep 28, 2023
@mattverse
Copy link
Member

Status update: Working on a PR for improvising test cases and refactoring, will get is in as Korean Holidays are over!

@mattverse
Copy link
Member

#6630

mattverse and others added 2 commits October 5, 2023 12:20
* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
@czarcas7ic
Copy link
Member

I looked over this again, and everything except for not being able to reach steps 5-7 makes sense to me. Once we get an answer to this as well as tests if it is in fact reachable, I can approve

@czarcas7ic czarcas7ic dismissed their stale review October 5, 2023 18:16

Steps 5-7 appear unreachable

@czarcas7ic czarcas7ic added the A:backport/v20.x backport patches to v20.x branch label Oct 12, 2023
* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val
@github-actions github-actions bot added the C:CLI label Oct 13, 2023
Comment on lines +100 to +101
for _, existingDelegation := range delegations {
totalShares = totalShares.Add(existingDelegation.Shares)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait... is this the code for valset preference? I think this is fundamentally wrong. This needs to be based on the token amount, not the number of shares

@ValarDragon
Copy link
Member Author

I think the last bug should be fixed, but in a separate PR. Otw this PR looks good to go to me

Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with merging this and fixing the share-based issue in a separate PR

@AlpinYukseloglu AlpinYukseloglu merged commit e12011b into main Oct 13, 2023
1 check passed
@AlpinYukseloglu AlpinYukseloglu deleted the dev/valset-pref-unbond-issues branch October 13, 2023 19:35
mergify bot pushed a commit that referenced this pull request Oct 13, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <[email protected]>
Co-authored-by: devbot-wizard <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: roman <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
(cherry picked from commit e12011b)
czarcas7ic pushed a commit that referenced this pull request Oct 13, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <[email protected]>
Co-authored-by: devbot-wizard <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: roman <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
(cherry picked from commit e12011b)

Co-authored-by: Dev Ojha <[email protected]>
pysel pushed a commit that referenced this pull request Oct 17, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <[email protected]>
Co-authored-by: devbot-wizard <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: roman <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v20.x backport patches to v20.x branch C:CLI C:docs Improvements or additions to documentation V:state/breaking State machine breaking PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fixing val-set-pref bugs
7 participants